package live.ifox.stage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.List;
import java.util.Map;

import live.ifox.factory.ID;
import live.ifox.helper.AdapterHelper;
import live.ifox.helper.PackageHelper;

public class AboutActivity extends AppCompatActivity {
    final int target_tick = 7;
    ListView listView;
    List<Map<String, Object>> infoModel;
    int tick;
    int level;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        tick = 0;
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(R.string.about);
        }

        level = getIntent().getIntExtra("level", 0);

        listView = findViewById(R.id.listView);
        listView.setOnItemClickListener(
                (parent, view, position, id) ->
                {
                    Map<String, Object> item = infoModel.get(position);
                    String label = String.valueOf(item.get("label"));
                    if (label.equals("versionName"))
                        checkTick();
                }
        );
        PackageHelper packageHelper = new PackageHelper(this);
        infoModel = packageHelper.getModel();
        SimpleAdapter adapter = AdapterHelper.getTableAdapter(this,
                infoModel, new String[]{"label", "detail"});
        listView.setAdapter(adapter);
    }

    void checkTick() {
        if (tick > target_tick)
            return;

        tick++;
        if (tick == target_tick) {
            EditText in_cipher = new EditText(this);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("请输入暗号").setView(in_cipher);
            builder.setPositiveButton(R.string.sure,
                    (paramAnonymousDialogInterface, paramAnonymousInt) ->
                    {
                        String cipher = in_cipher.getText().toString();
                        if (cipher.isEmpty())
                            return;

                        switch (cipher) {
                            case "dev++":
                                level = 1;
                                break;
                            case "dev--":
                                level = -1;
                                break;
                            default:
                                level = 0;
                                break;
                        }
                        setResult(ID.RESULT_DONE,
                                new Intent().putExtra("level", level));
                        finish();
                    }).setNegativeButton(R.string.cancel,
                    (paramAnonymousDialogInterface, paramAnonymousInt) -> {
                    });
            builder.create().show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
